package uestc.lj.gulishop.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.R;
import uestc.lj.common.vaild.AddGroup;
import uestc.lj.common.vaild.UpdateGroup;
import uestc.lj.common.vaild.UpdateStatusGroup;
import uestc.lj.gulishop.product.entity.BrandEntity;
import uestc.lj.gulishop.product.service.BrandService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 品牌
 *
 * @author crazlee
 * @email peterlee0@163.com
 * @date 2021-05-21 18:45:10
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌信息
     *
     * @return
     */
    @GetMapping("/infos")
    @Cacheable(value = "attr", key = "'brandInfo:'+#root.args")
    public R getBrands(@RequestParam("brandIds") List<Long> brandIds) {
        List<BrandEntity> brandEntityList = brandService.getBrandsByIds(brandIds);
        return R.ok().put("data", brandEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateByIdDetail(brand);
        return R.ok();
    }

    /**
     * 修改商品状态
     *
     * @param brand
     * @return
     */
    @RequestMapping("/update/status")
    //  @RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
